package leetcode;

public class ValidNumber {

	public static void main(String[] args) {
		ValidNumber object = new ValidNumber();
		
		String s0 = "0";
		String s1 = " 023  ";
		String s2 = "0.1";
		String s3 = "a12";
		String s4 = "1e10";
		String s5 = "5047e+6";
		System.out.println(object.isNumber(s0));
		System.out.println(object.isNumber(s1));
		System.out.println(object.isNumber(s2));
		System.out.println(object.isNumber(s3));
		System.out.println(object.isNumber(s4));
		System.out.println(object.isNumber(s5));
	}
	
	//finally AC
	
	public boolean isNumber(String s) {
		if(s == null || s.length() <= 0){
			return false;
		}
		char[] chas = s.trim().toCharArray();
		int begin = 0;
		if(chas[0] == '-' || chas[0] == '+'){
			begin = 1;
		}
		//注意，有小数，科学记数法，还有数字的可能
		int ditCount = 0;
		int eCount = 0;
		for (int i = begin; i < chas.length; i++) {
			if(chas[i] < '0' || chas[i] > '9'){
				//e、E、.前面必须有数字
//				|| (chas[i - 1] < '0' || chas[i] > '9'))
				//测试用例中.1是true，3.是true
				//但是.是false
				if( ( (i != begin)  && (chas[i] == 'e' || 
						chas[i] == 'E') ) || chas[i] == '.'){
					//后面必须是数字
					if(chas[i] != '.'){
						//可能有e的负数次方
						if(i == chas.length - 1){
							return false;
						}
						int nextNumber = i + 1;
						System.out.println("nextChar: " + chas[nextNumber]);
						if(chas[nextNumber] == '-' || chas[nextNumber] == '+'){
						    nextNumber = i + 2;
						    i++;
						}
						if(nextNumber > chas.length - 1 || (chas[nextNumber] < '0' || chas[nextNumber] > '9')){
							System.out.println("isthis:" );
						    return false;
						}
					}else{
						//不能只有一个.
						if(i == begin ){
							if(i == chas.length - 1 || ((chas[i + 1] < '0' || chas[i + 1] > '9') && (chas[i] != 'e' || 
						            chas[i] != 'E') ) ){
								System.out.println("there is ");
								return false;
							}
						}
						ditCount++;
						if(eCount == 1){
							return false;
						}
					}
					//E的后面不能出现点
					if(ditCount > 1){
						return false;
					}
					if(eCount > 1){
						return false;
					}
				}else{
					return false;
				}
			}
		}
		return true;
	}
}
